package cn.tedu.weibo1.controller;

import cn.tedu.weibo1.base.response.JsonResult;
import cn.tedu.weibo1.mapper.CommentMapper;
import cn.tedu.weibo1.pojo.dto.CommentDTO;
import cn.tedu.weibo1.pojo.entity.Comment;
import cn.tedu.weibo1.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;


@Slf4j
@RestController
@RequestMapping("v1/comment")
@Api(tags = "评论模块")
public class CommentController {
    @Autowired(required = false)
    private CommentMapper commentMapper;


    @PostMapping("insert")
    @ApiOperation(value = "发布评论")
    public JsonResult insertComment(@RequestBody @Validated CommentDTO commentDTO, @ApiIgnore HttpSession session){
        log.debug("commentDTO:"+commentDTO);
        UserVO userVO =(UserVO) session.getAttribute("user");
        Comment comment=new Comment();
        comment.setContent(commentDTO.getContent());
        comment.setWeiboId(commentDTO.getWeiboId());
        comment.setUserId(userVO.getId());
        int i = commentMapper.insertComment(comment);
        if (i==1)
            return JsonResult.ok();
        return JsonResult.notok();
    }


    @GetMapping("selectByWeiboId")
    @ApiOperation(value = "获取评论")
    public JsonResult selectAllComment(Long id){
        return JsonResult.ok(commentMapper.selectByWeiboId(id));
    }


}
